#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "QMLObject.h"
#include "new.h"
#include "hash_list.h"
#include "hash.h"

#define MAX_LEN (256)

static void *QMLObject_ctor(void *_self, va_list *app) 
{
    struct QMLObject *self = ((const struct Class *)HashTable)->ctor(_self, app);
	self->ValidPropertiesString = va_arg(*app, char*);
    return self;
}

static void *QMLObject_dtor(void* _self)
{
	struct QMLObject *self = ((const struct Class *)HashTable)->dtor(_self);
	return self;
}

void Draw(Pointer item)
{
	unsigned int i;
	struct HashList *buf;
	struct QMLObject *self = item;
	for(i = 0; i < (&(self->_))->size; i++)
	{
		buf = (&(self->_))->table[i];
		while(buf != NULL)
		{
			if((!strcmp((&(buf->_))->key, "id")) || (!strcmp((&(buf->_))->key, "type")) || (!strcmp((&(buf->_))->key, "color")))
			{
				printf("%s: %s \n", (&(buf->_))->key, (&(buf->_))->data);
			}
			else
			{
				printf("%s: %d \n", (&(buf->_))->key, *(int*)((&(buf->_))->data));
			}
			buf = buf->next;
		}
	}
}


int Parse(FILE* pFile, Pointer Dest)
{
	char* key;
	char* valuestring;
	char* readstring;
	char* teststring;
	unsigned int i = 0, j = 0, count = 0, pos_i, pos_j, *value;
	const char digits[11] = "0123456789";
	struct QMLObject *self = (struct QMLObject*)Dest;
	value = (int*)calloc(1, sizeof(int));
	readstring = (char*)calloc(MAX_LEN, sizeof(char));
	while(fgets(readstring, MAX_LEN, pFile))
	{
		if(!strcmp(readstring, "{\n"))
		{
			i++;
			pos_i = count;
		}
		if(!strcmp(readstring, "}\n") || !strcmp(readstring, "}"))
		{
			j++;
			pos_j = count;
		}
		count++;
	}
	count = 0;
	if((i != 1) || (j != 1) || (pos_i > pos_j) || (pos_j - pos_i == 1))
	{
		return 0;
	}
	fseek(pFile, 0, SEEK_SET);
	fgets(readstring, MAX_LEN, pFile);
	while(strcmp(readstring, "{\n"))
	{
		fgets(readstring, MAX_LEN, pFile);
	}
	while(fgets(readstring, MAX_LEN, pFile) && strcmp(readstring, "}\n") && strcmp(readstring, "}"))
	{
		key = strtok(readstring, "	 \n");
		valuestring = strtok(NULL, "	 \n");
		teststring = strtok(NULL, "	 \n");
		if(teststring == NULL)
		{
			if(key[strlen(key) - 1] == ':')
			{
				key[strlen(key) - 1] = '\0';
				if(strstr((self->ValidPropertiesString), key))
				{
					if((valuestring[0] == '\"') && (valuestring[strlen(valuestring) - 1] == '\"'))
					{
						memmove(valuestring, valuestring + 1, strlen(valuestring));
						valuestring[strlen(valuestring) - 1] = '\0';
						ht_set(self, key, valuestring, strlen(valuestring) + 1);
						count ++;
					}
					else
					{
						for(i = 0; i < strlen(valuestring); i++)
						{
							for(j = 0; j < strlen(digits); j++)
							{
								if(valuestring[i] == digits[j])
								{
									break;
								}
							}
							if(j == strlen(digits))
							{
								break;
							}
						}
						if(i == strlen(valuestring))
						{
							*value = atoi(valuestring);
							ht_set(self, key, value, sizeof(int));
							count ++;
						}
					}
				}
			}
		}
	}
	free(value);
	free(readstring);
	return count;
}


static const struct Class _QMLObject = {
    sizeof(struct QMLObject),
    QMLObject_ctor,
    QMLObject_dtor
};

const void *QMLObject = &_QMLObject;